package test.base.kv;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.iq80.leveldb.WriteBatch;
import org.junit.Test;

import hyl.base.kv.cache.KvCacheBT;
import hyl.base.kv.cache.KvExCacheBB;
import hyl.base.kv.cache.QueueObject;
import hyl.core.MyCharset;
import hyl.core.MyDate;
import hyl.core.MyFun;
import hyl.core.data.IParse;
import hyl.core.reflect.MyKryo;
import hyl.core.run.IDoEachT;
import hyl.core.run.IDoMapT;

public class testcache {
	/**
	 * 
	 */
	class User implements Serializable {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		String name;
		int age;

		public User(String name_, int age_) {
			name = name_;
			age = age_;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}

		public String toString() {
			return MyFun.join("姓名:", name, ";年龄=", age);
		}

	}

	MyKryo kryo = MyKryo.getInstance();
	IParse<User> ups = new IParse<User>() {

		@Override
		public User b2t(byte[] 对象) {
			return kryo.deserialize(对象);

		}

		@Override
		public byte[] t2b(User 对象) {
			return kryo.serialize(对象);
		}

	};

	// @Test
	public void test1() {
		// 测试数据 cacheobject
		System.out.println("---map----");
		KvCacheBT<User> co = new KvCacheBT<User>("用户库", "测试map");
		co.set转换器(ups);
		co.put("小红".getBytes(), new User("小红", 28));
		co.put("小吕".getBytes(), new User("小吕", 18));
		System.out.println(co.get("小红".getBytes()));

		System.out.println("---map-批插入---");

		Map<byte[], byte[]> mapp = new HashMap<>();
		mapp.put("小敏".getBytes(), ups.t2b(new User("小敏", 12)));
		mapp.put("小宋".getBytes(), ups.t2b(new User("小宋", 13)));
		co.putMany(mapp);

		System.out.println("---map-批量获取---");
		byte[][] bb = new byte[][] { "小敏".getBytes(), "小宋".getBytes() };

		Map<byte[], byte[]> map = co.getMany(Arrays.asList(bb));
		map.forEach((k, v) -> System.out.println(MyFun.join(MyFun.bytes2U8str(k), ",", ups.b2t(v))));

		System.out.println("---map-批量查询并删除---");

		co.clear();
		mapp = new HashMap<>();
		mapp.put("初中_小敏".getBytes(), ups.t2b(new User("小敏", 12)));
		mapp.put("初中_小宋".getBytes(), ups.t2b(new User("小宋", 13)));
		co.putMany(mapp);
		IDoMapT<WriteBatch, Map.Entry<byte[], byte[]>> call = new IDoMapT<WriteBatch, Map.Entry<byte[], byte[]>>() {
			@Override
			public void run(WriteBatch bat, Entry<byte[], byte[]> value) throws Exception {
				System.out
						.println(MyFun.join("-1-", MyFun.bytes2U8str(value.getKey()), ",", ups.b2t(value.getValue())));
				bat.delete(value.getKey());
			}

		};
		co.batch("初中_".getBytes(), call);
		System.out.println("---map-批量查询---");
		mapp = new HashMap<>();
		mapp.put("初中_小日".getBytes(), ups.t2b(new User("小日", 15)));
		mapp.put("初中_小名".getBytes(), ups.t2b(new User("小名", 14)));
		co.putMany(mapp);
		IDoEachT<byte[], byte[]> call1 = new IDoEachT<byte[], byte[]>() {
			@Override
			public boolean isOver(byte[] key, byte[] val) throws Exception {
				System.out.println(MyFun.join("-2-", MyFun.bytes2U8str(key), ",", ups.b2t(val)));
				return false;
			}
		};

		co.like("初中_".getBytes(), call1);

		System.out.println("---queue----");
		QueueObject<User> qo = new QueueObject<User>("用户库", "测试queue");
		qo.push(new User("小红", 28));
		qo.push(new User("小吕", 18));

		System.out.println(qo.pull());
		System.out.println(qo.pull());
	}

	@Test
	public void test2() {
		KvExCacheBB ecBytes = new KvExCacheBB("测试", "group1", 3000);
		// System.out.println(ecBytes._Em );
		ecBytes.set过期函数2((k, v) -> {
			// System.out.println(" -22- "+new String(k));
		});
		ecBytes.put("小红_".getBytes(), "{初中_}".getBytes(MyCharset._UTF8));
		ecBytes.put("小dd_".getBytes(), "{初中_}".getBytes(MyCharset._UTF8));
		System.out.println("--1--");
		MyDate.sleep(1001);

		String s = MyFun.bytes2Str(ecBytes.getBytes("小红_".getBytes()), MyCharset._UTF8);

		System.out.println("--2--" + s);

		MyDate.sleep(3101);

		s = MyFun.bytes2Str(ecBytes.getBytes("小红_".getBytes()), MyCharset._UTF8);

		System.out.println("--3--" + s);
		MyDate.sleep(61010);
	}

}
